﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;
using System.Collections.Specialized;
using Microsoft.SqlServer.Management.Common;
using System.Threading;
using System.Reflection;

namespace QLCF.Forms
{
    public partial class frmPrepareAppication : Form
    {
        public frmPrepareAppication()
        {
            InitializeComponent();
            lblMess.Text = "Đang khởi tạo...";              
        }
        private void frmPrepareAppication_Load(object sender, EventArgs e)
        {
            Thread checkDB = new Thread(CheckConnectDB);
            checkDB.Start();            
        }
        private void CheckConnectDB()
        {
            try
            {
                SetControlPropertyValue(lblMess,"Text","Kiểm tra dữ liệu...");
                //check database exists
                String DatabaseName = "QLCF";

                Server SqlServer = new Server(Utils.Setting.ServerName);
                ServerConnection SqlServerConnection = SqlServer.ConnectionContext;

                SqlServerConnection.LoginSecure = true;
                SqlServerConnection.DatabaseName = "master";
                SqlServerConnection.ConnectTimeout = 15;                
                if (SqlServer.Databases[DatabaseName] == null)
                {
                    SetControlPropertyValue(lblMess, "Text", "Khởi tạo dữ liệu...");
                    Database NewDatabase = new Database(SqlServer, DatabaseName);

                    FileGroup DatabaseFileGroup = new FileGroup(NewDatabase, "PRIMARY");
                    NewDatabase.FileGroups.Add(DatabaseFileGroup);

                    DataFile DatabaseDataFile = new DataFile(DatabaseFileGroup, DatabaseName);
                    DatabaseFileGroup.Files.Add(DatabaseDataFile);

                    DatabaseDataFile.FileName = Application.StartupPath + "\\data\\" + DatabaseName + ".mdf";

                    LogFile DatabaseLogFile = new LogFile(NewDatabase, DatabaseName + "_log");
                    NewDatabase.LogFiles.Add(DatabaseLogFile);

                    DatabaseLogFile.FileName = Application.StartupPath + "\\data\\" + DatabaseName + "_log.ldf";

                    StringCollection DatabaseFilesCollection = new StringCollection();

                    DatabaseFilesCollection.Add(DatabaseDataFile.FileName);
                    DatabaseFilesCollection.Add(DatabaseLogFile.FileName);

                    SqlServer.AttachDatabase(DatabaseName, DatabaseFilesCollection);
                    SetControlPropertyValue(lblMess, "Text", "Đã khởi tạo dữ liệu...");
                }
                SetControlPropertyValue(lblMess, "Text", "Kết nối dữ liệu...");
                SqlServerConnection.Cancel();
                SetControlPropertyValue(this, "Visible", false);
                PerformShowLoginForm(this);
                //frmLogin login = new frmLogin();
                //Application.Run(login);
            }
            catch
            {
                PerformAlert(this);
            }            
        }
        delegate void ShowLoginForm(Control oControl);
        private void PerformShowLoginForm(Control oControl)
        {
            if (oControl.InvokeRequired)
            {
                ShowLoginForm d = new ShowLoginForm(PerformShowLoginForm);
                oControl.Invoke(d, new object[] { oControl });
            }
            else
            {
                oControl.Hide();
                frmLogin login = new frmLogin();
                login.Show();
                //Application.Run(login);
            }
        }
        delegate void Alert(Control oControl);
        private void PerformAlert(Control oControl)
        {
            if (oControl.InvokeRequired)
            {
                Alert d = new Alert(PerformAlert);
                oControl.Invoke(d, new object[] { oControl });
            }
            else
            {                
                oControl.Hide();                
                MessageBox.Show("Lỗi khởi tạo cơ sở dữ liệu", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
        }
        delegate void SetControlValueCallback(Control oControl, string propName, object propValue);
        private void SetControlPropertyValue(Control oControl, string propName, object propValue)
        {
            if (oControl.InvokeRequired)
            {
                SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue);
                oControl.Invoke(d, new object[] { oControl, propName, propValue });
            }
            else
            {
                Type t = oControl.GetType();
                PropertyInfo[] props = t.GetProperties();
                foreach (PropertyInfo p in props)
                {
                    if (p.Name.ToUpper() == propName.ToUpper())
                    {
                        p.SetValue(oControl, propValue, null);
                    }
                }
            }
        }
    }
}
